package oop.exer2;

/**
 * Created with IntelliJ IDEA.
 * ClassName: StudentTest
 * Package: oop.exer2
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-11-10
 * Time: 22:37
 */

public class StudentTest {

    public static void main(String[] args) {
        //创建student[] 数组
        Student[] stu = new Student[20];

        //使用循环赋值
        for (int i = 0; i < stu.length; i++) {
            stu[i] = new Student();

            stu[i].number = i + 1;
            stu[i].state = (int) (Math.random() * 6 + 1);
            stu[i].score = (int) (Math.random() * 101);

        }

        //打印出三年级学生的信息
        for (int i = 0; i < stu.length; i++) {
            if (3 == stu[i].state) {
                System.out.println(stu[i].toString());
            }
        }

        System.out.println("排序之前的遍历");


        for (int i = 0; i < stu.length; i++) {
            System.out.println(stu[i].toString());
        }

        //使用冒泡排序 按学生成绩排序 并遍历所有学生信息
        for (int i = 0; i < stu.length - 1; i++) {
            for (int j = 0; j < stu.length - 1 - i; j++) {
                if (stu[j].score > stu[j+1].score){
                    //不是简单交换的值 而是交换的地址
                    Student tmp = stu[j];
                    stu[j] = stu[j+1];
                    stu[j+1] = tmp;
                }
            }
        }

        System.out.println("------------------------");
        System.out.println("排序之后的遍历");

        for (int i = 0; i < stu.length; i++) {
            System.out.println(stu[i].toString());
        }



    }

}
